#!/bin/bash

LOG_PATH=/var/log/chainlink/integration/logs
mkdir -p $LOG_PATH

assert () {
  sleepCount=0
  while [ "$sleepCount" -le "30" ] && output=`eval $2`; do
    if [ "$output" == "$3" ]; then
      printf -- "\033[32mTest passed!\033[0m $1: got expected value $3.\n"
      return
    fi
    sleepCount=$((sleepCount+1))
    sleep 1
  done
  printf -- "\033[31mTEST FAILED!\033[0m $1: expected $3, actual $output; while evaluating '$2'.\n"
  exit 1
}

waitFor () {
  [ -z "$2" ] && timeout=60 || timeout=$2
  sleepCount=0
  while [ "$sleepCount" -le "$timeout" ] && ! eval "$1" >/dev/null; do
      sleep 1
      sleepCount=$((sleepCount+1))
  done

  if [ "$sleepCount" -gt "$timeout" ]; then
    printf -- "\033[31mTimed out waiting for '$1' (waited ${timeout}s).\033[0m\n"
    exit 1
  fi
}

waitForResponse () {
  title "Waiting for $1."
  waitFor "curl -s \"$1\""
  title "Service on $1 is ready."
}

launch_chainlink() {
  waitForResponse $CHAINLINK_URL
  title "Chainlink is running."

  pushd tools/docker
  # Grab logs from our node service and find the unlocked account
  waitFor "docker logs chainlink-node 2>&1 | grep 'Unlocked account'" 10
  export CHAINLINK_NODE_ADDRESS=`docker logs chainlink-node 2>&1 | grep 'Unlocked account' | head -n 1 | awk '{print$5}'`

  # If we're using geth for this test
  # then we need to fund our hardcoded devnet address with the developer account given by geth
  if [ $GETH_MODE ]
  then
    export DEVELOPER_ACCOUNT=`docker logs geth 2>&1 | grep "Using developer account" | head -n 1 | awk -F "=" '{print$2}'`
    yarn workspace @chainlink/integration-scripts fund-address
  fi
  popd
}

deploy_contracts() {
  title "Migrating core contracts..."
  pushd integration >/dev/null

  # run migrations
  local log=$LOG_PATH/deploy.log
  yarn workspace @chainlink/integration-scripts deploy-contracts | tee $log
  export LINK_TOKEN_ADDRESS=`cat $log | grep 'Deployed LinkToken at:' | awk '{print$4}'`
  export ORACLE_CONTRACT_ADDRESS=`cat $log | grep 'Deployed Oracle at:' | awk '{print$4}'`
  export ETH_LOG_ADDRESS=`cat $log | grep 'Deployed EthLog at:' | awk '{print$4}'`
  export RUN_LOG_ADDRESS=`cat $log | grep 'Deployed RunLog at:' | awk '{print$4}'`

  echo "RunLog address: $RUN_LOG_ADDRESS"
  popd >/dev/null
  title "Migration complete."
}

deploy_v05_contracts() {
  title "Migrating v0.5 contracts..."
  local log=$LOG_PATH/deployv0.5.log
  yarn workspace @chainlink/integration-scripts deploy-v0.5-contracts | tee log
  export LINK_TOKEN_ADDRESS=`cat log | grep 'Deployed LinkToken at:' | awk '{print$4}'`
  export COORDINATOR_ADDRESS=`cat log | grep 'Deployed Coordinator at:' | awk '{print$4}'`
  export MEAN_AGGREGATOR_ADDRESS=`cat log | grep 'Deployed MeanAggregator at:' | awk '{print$4}'`
}

print_logs() {
  for log in $(find "$LOG_PATH" -maxdepth 1 -type f -iname '*.log'); do
    heading "$log"
    cat $log
  done
}

exit_handler() {
  errno=$?
  # Print all the logs if the test fails
  if [ $errno -ne 0 ]; then
    title "ABORTING TEST"
    printf -- "Exited with code $errno\n"
    print_logs
  fi
  exit $errno
}

title() {
  printf -- "\033[34m$1\033[0m\n"
}

heading() {
  printf -- "\n--------------------------------------------------------------------------------\n"
  title "$1"
  printf -- "--------------------------------------------------------------------------------\n\n"
}
